package com.qdlc.p2p.biz.web.listener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import com.qdlc.p2p.biz.service.LinkService;
import com.qdlc.p2p.dal.dto.Link;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.qdlc.p2p.biz.service.ArticleService;
import com.qdlc.p2p.biz.service.SystemConfigService;
import com.qdlc.p2p.biz.util.NotifyRequestUrl;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.constant.NotifyUrl;
import com.qdlc.p2p.common.enums.EnumActicityPlanNid;
import com.qdlc.p2p.dal.dao.BorrowConfigDao;
import com.qdlc.p2p.dal.dto.BorrowConfig;
import com.qdlc.p2p.dal.dto.Site;
import com.qdlc.p2p.dal.model.Tree;

public class WebConfigContextListener implements ServletContextListener, HttpSessionAttributeListener {
	private static Logger logger = Logger.getLogger(WebConfigContextListener.class);

	@Override
	public void contextDestroyed(ServletContextEvent event) {
		
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {

		ServletContext context = event.getServletContext();
		ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);

		//友情链接
		LinkService linkService = (LinkService) ctx.getBean("linkService");
		List<Link> link =  linkService.findByType(1);
		context.setAttribute("link", link);
		
		// 文章
		ArticleService articleService = (ArticleService) ctx.getBean("articleService");
		Tree<Site> tree = articleService.getSiteTree();
		context.setAttribute("tree", tree);

		//设置配置文件到ServletContext中
		SystemConfigService systemConfigService = ctx.getBean(SystemConfigService.class);
		systemConfigService.setWebConfig();
		
		// 借款标配置信息
		BorrowConfigDao borrowConfigDao = (BorrowConfigDao) ctx.getBean("borrowConfigDao");
		List<BorrowConfig> borrowConfigList = borrowConfigDao.findAll();
		Map<Object, Object> map = new HashMap<Object, Object>();
        for(BorrowConfig borrowConfig :borrowConfigList){
            map.put(borrowConfig.getId(), borrowConfig);
        }
		Global.BORROW_CONFIG_MAP = map;
		Global.BORROW_CONFIG_LIST = borrowConfigList;

        // 缓存回调地址 以便可以手动解发回调
        Global.putCallBackUrl(NotifyUrl.RED_ENVELOPE_NOTIFY_COLLECT_URL); // 红包代收回调
        Global.putCallBackUrl(NotifyUrl.RED_ENVELOPE_NOTIFY_PAY_URL); // 红包代付回调
        Global.putCallBackUrl(NotifyUrl.TENDER_NOTIFY_COLLECT_URL); // 投标代付回调
        Global.putCallBackUrl(NotifyUrl.REPAY_NOTIFY_PAY_URL); // 还款代付回调
        Global.putCallBackUrl(NotifyRequestUrl.ACTION_WITH_DRAW_URL);	//提现回调

        // 启用的活动环节配置(后台用)
        Map<Object, Object> teachMaps = new HashMap<Object, Object>();
        // 注册环节
        teachMaps.put(EnumActicityPlanNid.REGISTER.getValue(),EnumActicityPlanNid.REGISTER.getName());
        // 好友邀请环节
        teachMaps.put(EnumActicityPlanNid.INVITE.getValue(),EnumActicityPlanNid.INVITE.getName());
        // 投标成功环节
        teachMaps.put(EnumActicityPlanNid.TENDER.getValue(),EnumActicityPlanNid.INVITE.getName());
        // 满审成功环节
        teachMaps.put(EnumActicityPlanNid.VERIFY_FULL_BORROW_SUCCESS.getValue(),EnumActicityPlanNid.VERIFY_FULL_BORROW_SUCCESS.getName());
        // 投N月投标成功环节
        teachMaps.put(EnumActicityPlanNid.BY_MONTH_TENDER_SUCCESS.getValue(),EnumActicityPlanNid.BY_MONTH_TENDER_SUCCESS.getName());
		// 首次投标环节
		teachMaps.put(EnumActicityPlanNid.FIRST_TENDER.getValue(),EnumActicityPlanNid.FIRST_TENDER.getName());
		//大功告成奖
		teachMaps.put(EnumActicityPlanNid.BE_ACCOMPLISHED.getValue(),EnumActicityPlanNid.BE_ACCOMPLISHED.getName());
		//先声夺人奖
		teachMaps.put(EnumActicityPlanNid.SYNTHETIC_APERTURE.getValue(),EnumActicityPlanNid.SYNTHETIC_APERTURE.getName());
		//一掷千金奖
		teachMaps.put(EnumActicityPlanNid.GAMBLE_AT_HIGH_STAKES.getValue(),EnumActicityPlanNid.GAMBLE_AT_HIGH_STAKES.getName());
		//一掷千金奖
		teachMaps.put(EnumActicityPlanNid.AUTOMATIC_REBATE.getValue(),EnumActicityPlanNid.AUTOMATIC_REBATE.getName());

        context.setAttribute("activeTeach", teachMaps);
    }

	@Override
	public void attributeAdded(HttpSessionBindingEvent event) {
	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent event) {
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent event) {
	}
}
